package noth.li;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import java.io.*;
import java.util.*;

public class MyParseApp {

    //bilibili资源路径
    private static final String resourcePath = "D:\\doupocangqiong\\969230263";


    public static void main(String[] args) {
        listFiles();
    }


    public static void listFiles() {
        //1 连接到bilibili的本地视频存放库
        File dic = new File(resourcePath);

        //2 获得下层的所有目录
        /*
            目录结构为:
                -16
                    --audio.m4s
                    --index.json
                    --video.m4s
                -danmuku.xml
                -entry.json
         */
        File[] dics = dic.listFiles();


        //3 获取其中包含分集信息的entry.json
        //并将entry.json保存至list中
        List<File> entryJsonList = new ArrayList<>();
        for (File file : dics)
            entryJsonList.add(file.listFiles((dir, name) -> "entry.json".equals(name))[0]);


        //4 解析list中的所有entry.json,获取其中的分集信息
        TreeMap<String, File> map = new TreeMap<>(Comparator.comparingInt(o -> Integer.valueOf(o.toString())));
        for (File fff : entryJsonList) {
            try {
                System.out.println("当前文件===>" + fff);
                System.out.println("当前文件的父目录===>" + fff.getParentFile());
                System.out.println("当前文件的MP3文件的地址===>" + fff.getParentFile() + File.separator + "16" + File.separator + "audio.m4s");
                //读取文件内容,并通过alibaba的fastjson解析json字符串
                BufferedReader br = new BufferedReader(new FileReader(fff));
                Map maps = (Map) JSON.parse(br.readLine());
                JSONObject page_data = (JSONObject) maps.get("page_data");
                //分集信息
                String part = (String) page_data.get("part");
                br.close();
                System.out.println("当前集数解析前===>" + part);
                String indexCount = "";
                String substring = part.substring(part.indexOf("第") + 1, part.lastIndexOf("集"));
                System.out.println("当前文件名解析后===>" + substring);

                //多集
                if ((substring.length() > 4))
                    indexCount = substring.substring(0, substring.indexOf("-"));
                    //单集
                else
                    indexCount = substring;
                System.out.println("得到的集数数字===>" + indexCount);
                //Tree按照集数排序
                map.put(indexCount, new File(fff.getParentFile() + File.separator + "16" + File.separator + "audio.m4s"));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        //5.写入所有的文件
        try {
            Set set = map.keySet();
            for (Object o : set) {
                String index = (String) o;
                File parts = map.get(o);
                //获得mp3保存地址
                String writePath = parts.getParentFile().getParentFile().getParentFile().getParent() + File.separator + "mp3" + File.separator + "第" + index + "集.mp3";
                long start = System.currentTimeMillis();
                System.out.println("文件路径===>" + parts.getAbsolutePath());
                System.out.println("写出路径===>" + writePath);
                //FileUtils.copyFileToDirectory();
                //此处调用自己的文件写入方法,杜绝了不能直接写到文件中的弊病
                MyFileUtils.myCopyFileToDirectory(parts.getAbsoluteFile(), new File(writePath), true);
                long end = System.currentTimeMillis();
                System.out.println(writePath + "耗费时间" + (end - start) + "毫秒");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
